package com.java.web;

import com.java.bean.User;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: wjr
 * @Description:
 * @Date Created in  2020-09-09 0:11
 * @Modified By:
 */
@WebServlet(value = "/getuser")
public class GetUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int uid = Integer.parseInt(req.getParameter("uid"));
        User user = new User();
        for (int i = 0; i < 5; i++) {
            if (uid == i){
                user.setId(uid);
                user.setName("张三"+i);
                break;
            }
        }
        if (uid == 1){
            user.setId(uid);
            user.setName("张三");
        }
        JSONObject jsonObject = JSONObject.fromObject(user);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(jsonObject);
    }
}
